package cc.javaee.bbs.aspect;

import cc.javaee.bbs.annotations.Task;
import cc.javaee.bbs.task.AbstractJobTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author xdw
 */
@Aspect
@Component
public class JobCoreAspect {

    @Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled) && this(job)")
    public void proxyAspect(AbstractJobTemplate job) {
    }

    @Around("proxyAspect(job)")
    public void test(ProceedingJoinPoint pjp, AbstractJobTemplate job) {
        Signature signature = pjp.getSignature();
        if (signature instanceof MethodSignature) {
            Task task = ((MethodSignature) signature).getMethod()
                    .getAnnotation(Task.class);
            if (task != null) {
                job.templateMethod(task.value());
                return;
            }
        }
        job.templateMethod(job.getClass().getName());
    }


}
